package com.yms.controller;


import com.yms.pojo.PageResult;
import com.yms.pojo.Result;
import com.yms.pojo.Role;
import com.yms.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-03-20
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService iRoleService;
    //分页查询角色信息
    @RequestMapping("roleInfo")
    @ResponseBody
    public PageResult<Role> roleInfo(Integer page,Integer rows){
        return iRoleService.selectByPage(page,rows);
    }

    //增加角色及增加角色菜单
    @RequestMapping("addRoleAndRoleMenu")
    @ResponseBody
    public Result addRoleAndRoleMenu(Role role, String mid) {
        String[] split = mid.split(",");
        int[] a = new int[split.length];
        for (int i = 0; i <split.length; i++) {
            a[0]=Integer.parseInt(split[0]);
        }
        return iRoleService.addRoleAndRoleMenu(role,a);
    }
    //更新角色表及角色菜单表
    @RequestMapping("flushRoleAndRoleMenu")
    @ResponseBody
    public Result flushRoleAndRoleMenu(Role role,String mid){
        String[] split = mid.split(",");
        int[] a = new int[split.length];
        for (int i = 0; i <split.length; i++) {
            a[0]=Integer.parseInt(split[0]);
        }
        return iRoleService.flushRoleAndRoleMenu(role,a);
    }
    //根据rid删除角色表数据，及删除角色菜单表的数据
    @RequestMapping("deleteRole")
    @ResponseBody
    public Result deleteRole(String rid){
        String[] split = rid.split(",");
        int[] a = new int[split.length];
        for (int i = 0; i <split.length; i++) {
            a[0]=Integer.parseInt(split[0]);
        }
        return iRoleService.deleteRole(a);
    }
}

